<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随机数生成与操作</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f5f5f5;
        }

        .container {
            width: 500px;
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        h1 {
            text-align: center;
            margin-bottom: 20px;
            color: #333;
        }

        .button-group {
            display: flex;
            justify-content: center;
            margin-bottom: 20px;
            flex-wrap: wrap;
        }

        button {
            padding: 8px 12px;
            margin: 5px;
            border: 1px solid #ddd;
            border-radius: 4px;
            background-color: #f0f0f0;
            cursor: pointer;
            font-size: 14px;
        }

        button:hover {
            background-color: #e0e0e0;
        }

        textarea {
            width: 100%;
            height: 100px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: none;
            font-family: monospace;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>随机数生成与操作</h1>
        <div class="button-group">
            <button id="generateBtn">随机产生20个整数</button>
            <button id="sortBtn">从小到大排序并输出</button>
            <button id="findBtn">找出能被5整除的整数</button>
            <button id="clearBtn">清除</button>
        </div>
        <textarea id="outputText" readonly></textarea>
    </div>

    <script>
        // 获取DOM元素
        const generateBtn = document.getElementById('generateBtn');
        const sortBtn = document.getElementById('sortBtn');
        const findBtn = document.getElementById('findBtn');
        const clearBtn = document.getElementById('clearBtn');
        const outputText = document.getElementById('outputText');

        // 存储随机生成的数组
        let randomNumbers = [];

        // 生成随机数并显示
        generateBtn.addEventListener('click', function() {
            // 生成20个4位随机整数
            randomNumbers = [];
            for (let i = 0; i < 20; i++) {
                randomNumbers.push(Math.floor(Math.random() * 9000) + 1000);
            }

            // 显示随机数
            outputText.value = randomNumbers.join('、');
        });

        // 排序并输出
        sortBtn.addEventListener('click', function() {
            if (randomNumbers.length === 0) {
                alert('请先生成随机数');
                return;
            }

            // 排序数组
            const sortedNumbers = [...randomNumbers].sort((a, b) => a - b);

            // 显示排序后的结果
            outputText.value = sortedNumbers.join('、');
        });

        // 找出能被5整除的整数
        findBtn.addEventListener('click', function() {
            if (randomNumbers.length === 0) {
                alert('请先生成随机数');
                return;
            }

            // 找出能被5整除的整数
            const divisibleByFive = randomNumbers.filter(num => num % 5 === 0);

            // 显示结果
            if (divisibleByFive.length > 0) {
                outputText.value = divisibleByFive.join('、');
            } else {
                outputText.value = '没有能被5整除的整数';
            }
        });

        // 清除内容
        clearBtn.addEventListener('click', function() {
            outputText.value = '';
            randomNumbers = [];
        });
    </script>
</body>
</html>